package hander

import (
	"context"
	"fmt"
	"math/rand"
	"rpc-server/basic/global"
	"rpc-server/model"
	"rpc-server/proto"
	"time"
)

type UserServer struct {
	__.UnimplementedUserServer
}

func (c *UserServer) SendSms(_ context.Context, in *__.SendSmsRequest) (*__.SendSmsResponse, error) {

	code := rand.Intn(9000) + 1000
	global.Rdb.Set(context.Background(), "SendSms"+in.Mobile, code, time.Minute*5)

	global.Rdb.Expire(context.Background(), "SensSms"+in.Mobile, time.Minute*1)

	return &__.SendSmsResponse{}, nil
}

func (c *UserServer) Login(_ context.Context, in *__.LoginRequest) (*__.LoginResponse, error) {

	users := model.User{
		Name:     "用户" + in.Mobile,
		Nickname: "默认",
		Mobile:   in.Mobile,
		Email:    "null",
		Address:  "上海",
	}
	if err := global.DB.Create(&users).Error; err != nil {
		return nil, fmt.Errorf("注册失败")
	}
	get := global.Rdb.Get(context.Background(), "SendSms"+in.Mobile)

	if in.Mobile == get.Val() {
		return nil, fmt.Errorf("验证码错误")
	}
	return &__.LoginResponse{
		Id: int64(users.Id),
	}, nil
}

func (c *UserServer) Info(ctx context.Context, in *__.InfoRequest) (*__.InfoResponse, error) {
	var user model.User
	if err := global.DB.Where("id = ?", in.Id).First(&user).Error; err != nil {
		return nil, fmt.Errorf("用户不存在")
	}

	return &__.InfoResponse{
		Name:             user.Name,
		Nickname:         user.Nickname,
		Mobile:           user.Mobile,
		Email:            user.Email,
		Address:          user.Address,
		Set:              int64(user.Set),
		Age:              int64(user.Age),
		LikesNumbers:     int64(user.LikesNumbers),
		NumberOfLikes:    int64(user.NumberOfLikes),
		NumberOfComments: int64(user.NumberOfComments),
		NumberOfShares:   int64(user.NumberOfShares),
	}, nil

}
func (c *UserServer) AddMusic(ctx context.Context, in *__.AddMusicRequest) (*__.AddMusicResponse, error) {
	user := model.Music{
		Music: in.Music,
	}
	if err := global.DB.Create(&user).Error; err != nil {
		return nil, fmt.Errorf("音乐选择失败")
	}

	return &__.AddMusicResponse{
		Code: 500,
		Msg:  "音乐选择成功",
	}, nil

}
func (c *UserServer) AddWork(ctx context.Context, in *__.AddWorkRequest) (*__.AddWorkResponse, error) {
	user := model.Works{
		Name:      in.Name,
		Describe:  in.Describe,
		Ipaddress: in.Ipaddress,
		Music:     in.Music,
		Video:     in.Video,
		Img:       in.Img,
	}
	if err := global.DB.Create(&user).Error; err != nil {
		return nil, fmt.Errorf("作品发布失败")
	}

	return &__.AddWorkResponse{
		Code: 500,
		Msg:  "作品发布成功",
	}, nil

}

func (c *UserServer) Like(ctx context.Context, in *__.LikeRequest) (*__.LikeResponse, error) {
	var user model.Works
	if err := global.DB.Updates(&user).Error; err != nil {
		return nil, fmt.Errorf("作品发布失败")
	}

	return &__.LikeResponse{
		Code: 500,
		Msg:  "作品发布成功",
	}, nil
}
func (c *UserServer) AddCommit(_ context.Context, in *__.AddCommitRequest) (*__.AddCommitResponse, error) {
	user := model.CommentAssociation{
		UserId:  int(in.UserId),
		WorksId: int(in.WorkId),
		Content: in.Content,
	}
	if err := global.DB.Create(&user).Error; err != nil {
		return nil, fmt.Errorf("评论发布失败")
	}

	return &__.AddCommitResponse{
		Code: 500,
		Msg:  "评论发布成功",
	}, nil
}
